if CLIENT then

	/* Callback - Get team info */
	local function GetTeamInfo( data )
	
		/* Variables */
		local tab = {}
		
		/* Read contents */
		local id			= data:ReadShort()
		local name			= data:ReadString()
		local color			= color.FromString( data:ReadString() )
		local maxPercent	= data:ReadShort()
		local chatName		= data:ReadString()
		local defLoadout	= data:ReadShort()
		local createTime	= data:ReadLong()
		
		/* Debug output */
		DEBUG:Msg( "ID: " .. tostring( id ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Name: " .. tostring( name ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Color: " .. table.ToString( color ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Max percent: " .. tostring( maxPercent ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Chat name: " .. tostring( chatName ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Default loadout: " .. tostring( defLoadout ), "umsg.GetTeamInfo" )
		DEBUG:Msg( "Create time: " .. TimeToDateTimeString( createTime ), "umsg.GetTeamInfo" )
		
		/* If the team doesn't exist, create it */
		if not team.Valid( id ) then
			team.SetUp( id, name, color )
			
			/* Debug output */
			DEBUG:Msg( "Team create on client: " .. tostring( id ) .. ", " .. name .. ", " .. table.ToString( color ), "umsg.GetTeamInfo" )
		end
		
		/* Update team members */
		local tm			= team.GetInfo( id )
		tm.Name				= name
		tm.Color			= color
		tm.MaxPercent		= maxPercent
		tm.DefaultLoadout	= Loadout( defLoadout, "EmptyLoadout", nil, nil, nil, nil, nil, nil )
		tm.CreateTime		= createTime
		
		/* Ask for loadout */
		LocalPlayer():ConCommand( "bel_get_loadout " .. tostring( defLoadout ) )
		
		/* Debug output */
		DEBUG:Msg( "Team " .. chatName .. " has been successfully updated on client", "umsg.GetTeamInfo" )
	end
	
	/* Add a hook for GetTeamInfo */
	usermessage.Hook( "GetTeamInfo", GetTeamInfo )
	
	/* Callback - Get loadout */
	local function GetLoadout( data )
	
		/* Variables */
		local tab = {}
		
		/* Read contents */
		local id			= data:ReadShort()
		local name			= data:ReadString()
		local primary		= data:ReadShort()
		local secondary		= data:ReadShort()
		local inv1			= data:ReadShort()
		local inv2			= data:ReadShort()
		local backpack1		= data:ReadShort()
		local backpack2		= data:ReadShort()
		
		/* Debug output */
		DEBUG:Msg( "ID: " .. tostring( id ), "umsg.GetLoadout" )
		DEBUG:Msg( "Name: " .. tostring( name ), "umsg.GetLoadout" )
		DEBUG:Msg( "Primary: " .. tostring( primary ), "umsg.GetLoadout" )
		DEBUG:Msg( "Secondary: " .. tostring( secondary ), "umsg.GetLoadout" )
		DEBUG:Msg( "Inventory 1: " .. tostring( inv1 ), "umsg.GetLoadout" )
		DEBUG:Msg( "Inventory 2: " .. tostring( inv2 ), "umsg.GetLoadout" )
		DEBUG:Msg( "Backpack 1: " .. tostring( backpack1 ), "umsg.GetLoadout" )
		DEBUG:Msg( "Backpack 2: " .. tostring( backpack2 ), "umsg.GetLoadout" )
		
		/* Get the player's team */
		local tm			= team.GetInfo( LocalPlayer():Team() )
		
		/* Create a loadout instance if necessary */
		if not IsValidInstance( tm.DefaultLoadout, Loadout ) then
			tm.DefaultLoadout = Loadout( id, name, nil, nil, nil, nil, nil, nil )
		else
			tm.DefaultLoadout.ID = id
			tm.DefaultLoadout.Name = name
		end
		
		/* Ask for the objects */
		LocalPlayer():ConCommand( "bel_get_loadout_object primary " .. tostring( primary ) )
		LocalPlayer():ConCommand( "bel_get_loadout_object secondary " .. tostring( secondary ) )
		LocalPlayer():ConCommand( "bel_get_loadout_object inventory1 " .. tostring( inv1 ) )
		LocalPlayer():ConCommand( "bel_get_loadout_object inventory2 " .. tostring( inv2 ) )
		LocalPlayer():ConCommand( "bel_get_loadout_object backpack1 " .. tostring( backpack1 ) )
		LocalPlayer():ConCommand( "bel_get_loadout_object backpack2 " .. tostring( backpack2 ) )
		
		/* Debug output */
		DEBUG:Msg( "Loadout retrieved for team " .. tostring( tm.ChatName ), "umsg.GetLoadout" )
	end
	
	/* Add a hook for GetLoadout */
	usermessage.Hook( "GetLoadout", GetLoadout )
	
	/* Callback - Get loadout object */
	local function GetLoadoutObject( data )
	
		/* Variables */
		local tab = {}
		
		/* Read contents */
		local cls			= data:ReadString()
		local id			= data:ReadShort()
		local name			= data:ReadString()
		local health		= data:ReadShort()
		local weight		= data:ReadShort()
		local tp			= data:ReadShort()
		local reference		= data:ReadString()
		local refTp			= data:ReadShort()
		local price			= data:ReadShort()
		
		/* Debug output */
		DEBUG:Msg( "Object class: " .. tostring( cls ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "ID: " .. tostring( id ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Name: " .. tostring( name ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Health: " .. tostring( health ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Weight: " .. tostring( weight ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Type: " .. tostring( tp ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Reference: " .. tostring( reference ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Reference type: " .. tostring( refTp ), "umsg.GetLoadoutObject" )
		DEBUG:Msg( "Price: " .. tostring( price ), "umsg.GetLoadoutObject" )
		
		/* Get the player's team */
		local tm			= team.GetInfo( LocalPlayer():Team() )
		
		/* Create a loadout instance if necessary */
		if not IsValidInstance( tm.DefaultLoadout, Loadout ) then
			tm.DefaultLoadout = Loadout( id, name, nil, nil, nil, nil, nil, nil )
		else
			tm.DefaultLoadout.ID = id
			tm.DefaultLoadout.Name = name
		end
		
		/* Update loadout entry */
		local obj			= ObjectInfo( id, name, health, weight, nil, reference, nil, price )
		if cls == "primary" then tm.DefaultLoadout.Primary = obj
		elseif cls == "secondary" then tm.DefaultLoadout.Secondary = obj
		elseif cls == "inventory1" then tm.DefaultLoadout.Inventory1 = obj
		elseif cls == "inventory2" then tm.DefaultLoadout.Inventory2 = obj
		elseif cls == "backpack1" then tm.DefaultLoadout.Backpack1 = obj
		elseif cls == "backpack2" then tm.DefaultLoadout.Backpack2 = obj end
		
		/* Ask for type */
		LocalPlayer():ConCommand( "bel_get_object_type " .. cls .. " " .. tostring( tp ) )
		
		/* Ask for reference type */
		LocalPlayer():ConCommand( "bel_get_obj_ref_type " .. cls .. " " .. tostring( refTp ) )
		
		/* Debug output */
		DEBUG:Msg( "Loadout object retrieved for team " .. tostring( tm.ChatName ) .. " and item " .. cls, "umsg.GetLoadoutObject" )
	end
	
	/* Add a hook for GetLoadoutObject */
	usermessage.Hook( "GetLoadoutObject", GetLoadoutObject )
	
	/* Callback - Get loadout object type */
	local function GetLoadoutObjectType( data )
	
		/* Variables */
		local tab = {}
		
		/* Read contents */
		local cls			= data:ReadString()
		local id			= data:ReadShort()
		local name			= data:ReadString()
		local className		= data:ReadString()
		
		/* Debug output */
		DEBUG:Msg( "Object class: " .. tostring( cls ), "umsg.GetLoadoutObjectType" )
		DEBUG:Msg( "ID: " .. tostring( id ), "umsg.GetLoadoutObjectType" )
		DEBUG:Msg( "Name: " .. tostring( name ), "umsg.GetLoadoutObjectType" )
		DEBUG:Msg( "Class name: " .. tostring( className ), "umsg.GetLoadoutObjectType" )
		
		/* Get the player's team */
		local tm			= team.GetInfo( LocalPlayer():Team() )
		
		/* Create a loadout instance if necessary */
		if not IsValidInstance( tm.DefaultLoadout, Loadout ) then
			tm.DefaultLoadout = Loadout( id, name, nil, nil, nil, nil, nil, nil )
		else
			tm.DefaultLoadout.ID = id
			tm.DefaultLoadout.Name = name
		end
		
		/* Update loadout entry */
		local tp			= ObjectType( id, name, className )
		local obj			= ObjectInfo( id, name, health, weight, tp, reference, nil, price )
		if cls == "primary" then tm.DefaultLoadout.Primary = obj
		elseif cls == "secondary" then tm.DefaultLoadout.Secondary = obj
		elseif cls == "inventory1" then tm.DefaultLoadout.Inventory1 = obj
		elseif cls == "inventory2" then tm.DefaultLoadout.Inventory2 = obj
		elseif cls == "backpack1" then tm.DefaultLoadout.Backpack1 = obj
		elseif cls == "backpack2" then tm.DefaultLoadout.Backpack2 = obj end
		
		/* Debug output */
		DEBUG:Msg( "Loadout object type retrieved for team " .. tostring( tm.ChatName ) .. " and item " .. cls, "umsg.GetLoadoutObjectType" )
	end
	
	/* Add a hook for GetLoadoutObjectType */
	usermessage.Hook( "GetLoadoutObjectType", GetLoadoutObjectType )
	
	/* Callback - Print in chat */
	local function ChatMsg( data )
	
		/* Variables */
		local tab = {}
	
		/* Read the map name */
		local content = data:ReadString()
		
		/* Debug output */
		DEBUG:Msg( "Content: " .. content, "umsg.ChatMsg" )
		
		/* Split contents */
		local i = 1
		for e in string.gmatch( content, "[%w+|#%a+]+" ) do
			
			/* Debug output */
			DEBUG:Msg( "Element: " .. e, "umsg.ChatMsg" )
			
			/* Color ? */
			if string.match( e, "^#%d+" ) == e then
				
				/* Debug output */
				DEBUG:Msg( "Element is a color", "umsg.ChatMsg" )
			
				/* Get color members */
				local cm = nil
				if string.len( e ) == 4 then cm = string.gmatch( e, "%d" )
				elseif string.len( e ) == 7 then cm = string.gmatch( e, "%d%d" ) end
				
				/* If length was not correct, skip */
				if cm then
					local r,g,b
					for v in cm do
						if not r then r = v
						elseif not g then g = v
						elseif not b then b = v end
					end
					tab[i] = Color( tonumber( r ), tonumber( g ), tonumber( b ) )
					
					/* Debug output */
					DEBUG:Msg( "Color: " .. tostring(tab[i].r) .. ", " .. tostring(tab[i].g) .. ", " .. tostring(tab[i].b), "umsg.ChatMsg" )
				end
				
			/* Text ? */
			else
			
				/* Debug output */
				DEBUG:Msg( "Element is text", "umsg.ChatMsg" )
			
				/* Just add the string */
				tab[i] = tostring(e)
			end
			
			i = i + 1
		end
		
		/* Output text in player's chat */
		for i,v in ipairs(tab) do
			chat.AddText( v )
		end
	end
	
	/* Add a hook for ChatMsg */
	usermessage.Hook( "ChatMsg", ChatMsg )
	
	/* Callback - Test UMSG */
	local function TestUMSG( data )
		local str = data:ReadString()
		DEBUG:Msg( "TestUMSG: " .. str, "TestUMSG" )
		LocalPlayer().TestVar = "<empty>"
		LocalPlayer():ConCommand( "bel_get_umsg " .. str )
		DEBUG:Msg( "TestUMSG: " .. tostring( LocalPlayer().TestVar ), "TestUMSG" )
	end
	usermessage.Hook( "TestUMSG", TestUMSG )
	
	/* Callback - Get UMSG */
	local function GetUMSG( data )
		local str = data:ReadString()
		DEBUG:Msg( "GetUMSG: " .. str, "GetUMSG" )
		LocalPlayer().TestVar = str
	end
	usermessage.Hook( "GetUMSG", GetUMSG )
	
	/* Show team menu */
	local function ShowTeam( data )
	
		/* Frame */
		local popup = vgui.Create( "BelListBox" )
		popup:SetSize( ScrW() / 4 + 32, ScrH() / 3 )
		popup:Center()
		popup:SetDraggable( true )
		popup:Initialize()
		popup:SetTitle( "BELgui Preview" )
		popup.ShowHideFader:SetOutDuration( 2 )
		popup.WaitForChildren = true
		popup:MakePopup()
		
		/* Items */
		-- popup:AddItem( "One", function() LocalPlayer():ConCommand( "say", "One" ) end )
		-- popup:AddItem( "Two", function() LocalPlayer():ConCommand( "say", "Two" ) end )
		-- popup:AddItem( "Three", function() LocalPlayer():ConCommand( "say", "Three" ) end )
		
		/* Button */
		local button1 = vgui.Create( "BelButton", popup.ScrollPanel )
		button1:SetText( "One" )
		button1:Initialize()
		button1.ShowHideFader:SetInDuration( 0.75 )
		button1.ShowHideFader:SetOutDuration( 0.5 )		
		
		/* Button */
		local button2 = vgui.Create( "BelButton", popup.ScrollPanel )
		button2:SetText( "Two" )
		button2:Initialize()
		button2.ShowHideFader:SetInDuration( 0.75 )
		button2.ShowHideFader:SetOutDuration( 0.5 )
		
		/* Button */
		local button3 = vgui.Create( "BelButton", popup.ScrollPanel )
		button3:SetText( "Three" )
		button3:Initialize()
		button3.ShowHideFader:SetInDuration( 0.75 )
		button3.ShowHideFader:SetOutDuration( 0.5 )
		
		/* Check Button */
		local button4 = vgui.Create( "BelCheckButton", popup.ScrollPanel )
		button4:SetText( "Four" )
		button4:Initialize()
		button4.ShowHideFader:SetInDuration( 0.75 )
		button4.ShowHideFader:SetOutDuration( 0.5 )
		
		/* Check Button */
		local button5 = vgui.Create( "BelCheckButton", popup.ScrollPanel )
		button5:SetText( "Five" )
		button5:Initialize()
		button5.ShowHideFader:SetInDuration( 0.75 )
		button5.ShowHideFader:SetOutDuration( 0.5 )
		
		/* Register controls as list box items */
		popup:AddControl( button1 )
		popup:AddControl( button2 )
		popup:AddControl( button3 )
		popup:AddControl( button4 )
		popup:AddControl( button5 )
		
		/* Horizontal ? */
		-- popup:SetDirection( LIST_DIR_H )
		
		/* Show the menu */
		popup:SetVisible( true )
	end
	usermessage.Hook( "UMSG_ShowTeam", ShowTeam )
	
end